package com.gurb.android;

// NB. This is a quick and ugly proof of concept

import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Environment;

import android.view.View;
import android.util.Log;
import android.media.MediaPlayer;

import java.io.IOException;


public class CastBlastActivity extends ListActivity {

	private static final String LOG_TAG = "CastBlast";
	
	private MediaPlayer mPlayer = new MediaPlayer();
	protected boolean mStartPlaying = true;
	private boolean paused = false;

	private MusicList musicList = new MusicList(this);
	
	public CastBlastActivity() {
		Environment.getExternalStorageDirectory().getAbsolutePath();
	}
	
/** 
 *  TODO Need to do a select dialog for the casts directory... but not now
 * 
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		AlertDialog.Builder builder = new Builder(this);

		switch (id) {
		case DIALOG_LOAD_FILE:
			builder.setTitle("Choose your cast directory");
			if (mFileList == null) {
				Log.e(LOG_TAG,
						"Showing file picker before loading the file list");
				dialog = builder.create();
				return dialog;
			}
			builder.setItems(mFileList, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					System.err.println("WHAT");
				}
			});
			break;
		}
		dialog = builder.show();
		return dialog;
	}
	
	TODO Implement head phone out and in, phone call, etc interrupts
	
	TODO Have state so can continue playing 
**/
	
	private void onPlay(boolean start,String key) {
		if (start) {
			startPlaying(key);
		} else {
			pausePlaying();
		}
	}

	private void startPlaying(String key) {
		if (mPlayer == null) mPlayer = new MediaPlayer();
		if (paused){
			mPlayer.start();
			paused = false;
			return;
		}
		
		try {
			mPlayer.setDataSource(musicList.getFilePath(key));
			mPlayer.prepare();
			mPlayer.start();
			mPlayer.setOnCompletionListener(new FinishedPlayingListener(musicList,key));
			return;
		} catch (IOException e) {
			Log.e(LOG_TAG, "prepare() failed", e);
		}
	}

	private void stopPlaying() {
		mPlayer.release();
		mPlayer = null;
	}

	private void pausePlaying() {
		mPlayer.pause();
		paused = true;
	}

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

/**		
		TODO Pause, Rewind, Fast forward

        final Button pause = (Button) findViewById(R.id.pause_button);
        pause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                pausePlaying();
            }
        });

        final Button ff = (Button) findViewById(R.id.ff_button);
        ff.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                fastForward();
            }
        });


**/		
		
		// TODO delete button and icon in list
		this.setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, musicList.getList()));
	}
	

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
				.show();
		onPlay(mStartPlaying,keyword);
		mStartPlaying = !mStartPlaying;
	}
	
    @Override
	public void onPause() {
		super.onPause();
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
	}
}